<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="发文名称" prop="dispatchName">
				<el-input v-model="queryParams.dispatchName" placeholder="请输入发文名称" clearable @keyup.enter.native="handleQuery" maxlength="20" show-word-limit />
			</el-form-item>
			<el-form-item label="文件名称" prop="fileName">
				<el-input v-model="queryParams.fileName" placeholder="请输入文件名称" clearable @keyup.enter.native="handleQuery" maxlength="20" show-word-limit />
			</el-form-item>
			<el-form-item label="发文日期" prop="dispatchTime">
				<el-date-picker clearable v-model="queryParams.dispatchTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择发文日期" style="width: 160px"></el-date-picker>
			</el-form-item>
			<el-form-item label="发文类型" prop="salesType">
				<el-select v-model="queryParams.salesType" placeholder="请选择" style="width: 160px">
					<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="主送部门" prop="department">
				<el-input v-model="queryParams.department" placeholder="请输入主送部门" clearable @keyup.enter.native="handleQuery" maxlength="20" show-word-limit />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['erp:salesDocument:add']">新增</el-button>
			</el-col>
			<!-- <el-col :span="1.5">
				<el-button type="success"  icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['erp:salesDocument:edit']">修改</el-button>
			</el-col> -->
			<el-col :span="1.5">
				<el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['erp:salesDocument:remove']">删除</el-button>
			</el-col>
			<CreateForm :ids="ids" formtype="t_dispatchList" />
			<right-toolbar @changeTableData="changeTableData" menuNames="dispatchlists49" :showSearch.sync="showSearch" @queryTable="getList" perm="erp:dispatchList:index" :columns.sync="columns"></right-toolbar>
		</el-row>

		<div class="informationTab">
			<el-table ref="table" @row-click="row_click" v-loading="loading" :data="salesDocumentList" @selection-change="handleSelectionChange" border height="100%">
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column label="序号" align="center" type="index" width="60" />
				<el-table-column label="发文名称" align="center" prop="dispatchName" show-overflow-tooltip min-width="150" v-if="columns[0].visible" />
				<el-table-column label="文件名称" align="center" prop="fileName" show-overflow-tooltip min-width="150" v-if="columns[1].visible" />
				<el-table-column label="发文日期" align="center" prop="dispatchTime" width="120" v-if="columns[2].visible">
					<template slot-scope="scope">
						<span>{{ parseTime(scope.row.dispatchTime, "{y}-{m}-{d}") }}</span>
					</template>
				</el-table-column>
				<el-table-column label="发文类型" align="center" prop="salesType" width="120" v-if="columns[3].visible">
					<template slot-scope="scope">
						<span v-if="scope.row.salesType == '1'">服务保障</span>
						<span v-if="scope.row.salesType == '2'">产品保障</span>
						<span v-if="scope.row.salesType == '3'">设备保障</span>
					</template>
				</el-table-column>
				<el-table-column label="主送部门" align="center" prop="department" min-width="200" show-overflow-tooltip v-if="columns[4].visible" />
				<el-table-column label="备注" prop="remark" width="200" show-overflow-tooltip v-if="columns[5].visible" />
				<el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
					<template slot-scope="scope">
						<el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)" v-hasPermi="['erp:salesDocument:detail']">查看</el-button>
						<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['erp:salesDocument:edit']">修改</el-button>
						<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['erp:salesDocument:remove']">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>

		<pagination menuNames="Pagination58" v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

		<!-- 添加或修改销售发文单对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="90%" append-to-body v-dialogDrag :close-on-click-modal="false" :close-on-press-escape="false">
			<el-form ref="form" :model="form" :rules="rules" label-width="80px" :disabled="title.indexOf('查看') != -1">
				<el-row :gutter="20">
					<el-col :span="12">
						<el-form-item label="发文名称" prop="dispatchName">
							<el-input v-model="form.dispatchName" placeholder="请输入发文名称" maxlength="30" clearable show-word-limit />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="文件名称" prop="fileName">
							<el-input v-model="form.fileName" placeholder="请输入文件名称" maxlength="30" clearable show-word-limit />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="20">
					<el-col :span="12">
						<el-form-item label="发文日期" prop="dispatchTime">
							<el-date-picker clearable v-model="form.dispatchTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择日期" style="width: 100%"></el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="发文类型" prop="salesType">
							<el-select v-model="form.salesType" placeholder="请选择" style="width: 100%" @change="changeSalesType">
								<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>

				<el-row :gutter="20" v-if="title.indexOf('添加') == -1">
					<el-col :span="12">
						<el-form-item label="合计" prop="amountTotal">
							<el-input v-model="form.amountTotal" placeholder="请输入合计" maxlength="30" clearable readonly />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="项数" prop="number">
							<el-input v-model="form.number" placeholder="请输入项数" maxlength="30" clearable readonly />
						</el-form-item>
					</el-col>
				</el-row>
				<el-form-item label="主送部门" prop="department">
					<el-input v-model="form.department" placeholder="请输入主送部门" clearable maxlength="30" show-word-limit />
				</el-form-item>
				<el-form-item label="备注" prop="remark">
					<el-input v-model="form.remark" type="textarea" :rows="2" placeholder="请输入备注" clearable />
				</el-form-item>
			</el-form>

			<el-divider v-if="form.salesType"></el-divider>

			<template v-if="form.salesType === '1'">
				<div class="action-bar" v-if="title.indexOf('查看') == -1">
					<el-button type="primary" size="small" @click="addOne" style="margin-bottom: 12px">新增</el-button>
					<span class="tips" v-if="tableData.length > 0">（以下当中，售后服务报告表编号、服务费用、路费，三项为非必填项） </span>
				</div>

				<el-table :data="tableData" style="width: 100%" border>
					<el-table-column label="编号" align="center" type="index" width="60" />
					<el-table-column prop="serviceName" label="服务人员姓名" width="200" align="center">
						<template slot-scope="scope">
							<el-input v-model="scope.row.serviceName" maxlength="20" :disabled="title.indexOf('查看') != -1" />
						</template>
					</el-table-column>
					<el-table-column prop="serviceUser" label="服务用户" width="200" align="center">
						<template slot-scope="scope">
							<el-input v-model="scope.row.serviceUser" maxlength="20" :disabled="title.indexOf('查看') != -1" />
						</template>
					</el-table-column>
					<el-table-column prop="serviceDay" label="服务天数" width="90" align="center">
						<template slot-scope="scope">
							<el-input v-model="scope.row.serviceDay" maxlength="10" oninput="value=value.replace(/[^\d]/g,'')" :disabled="title.indexOf('查看') != -1" />
						</template>
					</el-table-column>
					<el-table-column prop="serviceContent" label="服务内容" width="200" align="center">
						<template slot-scope="scope">
							<el-input v-model="scope.row.serviceContent" maxlength="30" :disabled="title.indexOf('查看') != -1" />
						</template>
					</el-table-column>
					<el-table-column prop="serviceAfterSale" label="售后服务报告表编号" width="200" align="center">
						<template slot-scope="scope">
							<el-input v-model="scope.row.serviceAfterSale" maxlength="20" :disabled="title.indexOf('查看') != -1" />
						</template>
					</el-table-column>
					<el-table-column prop="tripMode" label="出行方式" width="180" align="center">
						<template slot-scope="scope">
							<el-input v-model="scope.row.tripMode" maxlength="20" :disabled="title.indexOf('查看') != -1" />
						</template>
					</el-table-column>
					<el-table-column prop="serviceCost" label="服务费用" width="160" align="center">
						<template slot-scope="scope">
							<el-input v-model="scope.row.serviceCost" maxlength="20" oninput="value=value.replace(/[^0-9.]/g,'')" :disabled="title.indexOf('查看') != -1" />
						</template>
					</el-table-column>
					<el-table-column prop="travellingExpenses" label="路费" width="160" align="center">
						<template slot-scope="scope">
							<el-input v-model="scope.row.travellingExpenses" maxlength="20" oninput="value=value.replace(/[^0-9.]/g,'')" :disabled="title.indexOf('查看') != -1" />
						</template>
					</el-table-column>
					<el-table-column prop="totalCost" label="合计" width="200" align="center">
						<template slot-scope="scope">
							<el-input v-model="scope.row.totalCost" maxlength="20" oninput="value=value.replace(/[^0-9.]/g,'')" :disabled="title.indexOf('查看') != -1" />
						</template>
					</el-table-column>
					<el-table-column prop="buyerName" label="购买方名称" width="300" align="center">
						<template slot-scope="scope">
							<el-input v-model="scope.row.buyerName" maxlength="40" :disabled="title.indexOf('查看') != -1" />
						</template>
					</el-table-column>
					<el-table-column prop="buyerParagraph" label="购买方税号" width="230" align="center">
						<template slot-scope="scope">
							<el-input v-model="scope.row.buyerParagraph" maxlength="30" oninput="value=value.replace(/[^\d]/g,'')" :disabled="title.indexOf('查看') != -1" />
						</template>
					</el-table-column>
					<el-table-column prop="remark" label="备注" width="230" align="center">
						<template slot-scope="scope">
							<el-input v-model="scope.row.remark" type="textarea" :rows="2" maxlength="30" :disabled="title.indexOf('查看') != -1" />
						</template>
					</el-table-column>
					<el-table-column label="操作" width="100" align="center">
						<template slot-scope="scope">
							<el-button type="danger" size="small" @click="deleteRow(scope.$index)" :disabled="title.indexOf('查看') != -1">删除</el-button>
						</template>
					</el-table-column>
				</el-table>
			</template>

			<template v-if="['2', '3'].includes(form.salesType)">
				<div class="action-bar" v-if="title.indexOf('查看') == -1">
					<el-button type="primary" size="small" @click="addOne" style="margin-bottom: 12px" v-if="form.salesType !== '2'">新增</el-button>
					<el-button type="primary" size="small" @click="openSecondDialog" style="margin-bottom: 12px" v-if="form.salesType === '2'">查询结算</el-button>
					<span class="tips" v-if="tableData.length > 0">（以下当中，单价、数量、金额（含税）、税率，四项为非必填项） </span>
				</div>

				<el-table :data="tableData" style="width: 100%" border>
					<el-table-column label="编号" align="center" type="index" width="60" />
					<el-table-column prop="productName" label="货物/应税劳务名称（产品名称）" width="280" align="center">
						<template slot-scope="scope">
							<el-input show-word-limit v-model="scope.row.productName" maxlength="30" :disabled="title.indexOf('查看') != -1" />
						</template>
					</el-table-column>
					<el-table-column prop="contractNumber" label="合同号" width="200" align="center">
						<template slot-scope="scope">
							<el-input v-model="scope.row.contractNumber" maxlength="20" :disabled="title.indexOf('查看') != -1" />
						</template>
					</el-table-column>
					<el-table-column prop="productModel" label="规格型号（件号）" width="150" align="center">
						<template slot-scope="scope">
							<el-input v-model="scope.row.productModel" maxlength="20" :disabled="title.indexOf('查看') != -1" />
						</template>
					</el-table-column>
					<el-table-column prop="serialNumber" label="序号" width="150" align="center">
						<template slot-scope="scope">
							<el-input v-model="scope.row.serialNumber" maxlength="30" :disabled="title.indexOf('查看') != -1" />
						</template>
					</el-table-column>
					<el-table-column prop="measuringUnit" label="计量单位" width="90" align="center">
						<template slot-scope="scope">
							<el-input v-model="scope.row.measuringUnit" maxlength="10" :disabled="title.indexOf('查看') != -1" />
						</template>
					</el-table-column>
					<el-table-column prop="unitPrice" label="单价" width="100" align="center">
						<template slot-scope="scope">
							<el-input v-model="scope.row.unitPrice" maxlength="20" oninput="value=value.replace(/[^0-9.]/g,'')" :disabled="title.indexOf('查看') != -1" />
						</template>
					</el-table-column>
					<el-table-column prop="quantity" label="数量" width="90" align="center">
						<template slot-scope="scope">
							<el-input v-model="scope.row.quantity" maxlength="20" oninput="value=value.replace(/[^0-9.]/g,'')" :disabled="title.indexOf('查看') != -1" />
						</template>
					</el-table-column>
					<el-table-column prop="contractAmount" label="金额（含税）" width="200" align="center">
						<template slot-scope="scope">
							<el-input v-model="scope.row.contractAmount" maxlength="20" oninput="value=value.replace(/[^0-9.]/g,'')" :disabled="title.indexOf('查看') != -1" />
						</template>
					</el-table-column>
					<el-table-column prop="tax" label="税率" width="200" align="center">
						<template slot-scope="scope">
							<el-input v-model="scope.row.tax" maxlength="20" oninput="value=value.replace(/[^0-9.]/g,'')" :disabled="title.indexOf('查看') != -1" />
						</template>
					</el-table-column>
					<el-table-column prop="buyerName" label="购买方名称" width="300" align="center">
						<template slot-scope="scope">
							<el-input v-model="scope.row.buyerName" maxlength="40" :disabled="title.indexOf('查看') != -1" />
						</template>
					</el-table-column>
					<el-table-column prop="buyerParagraph" label="购买方税号" width="230" align="center">
						<template slot-scope="scope">
							<el-input v-model="scope.row.buyerParagraph" maxlength="30" oninput="value=value.replace(/[^\d]/g,'')" :disabled="title.indexOf('查看') != -1" />
						</template>
					</el-table-column>
					<el-table-column prop="remark" label="备注" width="230" align="center">
						<template slot-scope="scope">
							<el-input v-model="scope.row.remark" type="textarea" :rows="2" maxlength="30" :disabled="title.indexOf('查看') != -1" />
						</template>
					</el-table-column>
					<el-table-column label="操作" width="100" align="center" v-if="form.salesType !== '2'">
						<template slot-scope="scope">
							<el-button type="danger" size="small" @click="deleteRow(scope.$index)" :disabled="title.indexOf('查看') != -1">删除</el-button>
						</template>
					</el-table-column>
				</el-table>
			</template>

			<div slot="footer" class="dialog-footer" v-if="title.indexOf('查看') == -1">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>

		<el-dialog title="查询结算" :visible.sync="dialogVisible" width="700px" :close-on-click-modal="false" :close-on-press-escape="false" v-dialogDrag :destroy-on-close="true" :before-close="handleClose">
			<el-table :data="settlementList" style="width: 100%" border ref="multiselectTable" @select="handleDeviceSelectionChange" @select-all="handleDeviceAllChange">
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column type="index" width="60" align="center" label="序号" />
				<el-table-column prop="settleAccountsName" label="结算名称" align="center"></el-table-column>
				<el-table-column prop="batch" label="批次" align="center"></el-table-column>
			</el-table>

			<pagination v-show="settlementListTotal > 0" :total="settlementListTotal" :page.sync="queryParamsSettlementList.pageNum" :limit.sync="queryParamsSettlementList.pageSize" @pagination="getData" />

			<span slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitSeletedItem">确 定</el-button>
				<el-button @click="dialogVisible = false">取 消</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
import CreateForm from "@/views/components/CreateForm"
import { listSalesDocument, getSalesDocument, delSalesDocument, addSalesDocument, updateSalesDocument, getSettlementList, getSublist } from "@/api/synthesize/dispatchList/index.js"
import { getcolumn } from "@/api/selectList"
export default {
	components: { CreateForm },
	name: "DispatchList",
	data() {
		return {
			// 列信息
			columns: [
				{ key: 0, label: `发文名称`, visible: true },
				{ key: 1, label: `文件名称`, visible: true },
				{ key: 1, label: `发文日期`, visible: true },
				{ key: 1, label: `发文类型`, visible: true },
				{ key: 1, label: `主送部门`, visible: true },
				{ key: 1, label: `备注`, visible: true }
			],
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 销售发文单表格数据
			salesDocumentList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				dispatchName: null,
				fileName: null,
				dispatchTime: null,
				department: null,
				number: null,
				amount: null,
				amountTotal: null,
				pagesNumber: null,
				salesType: null,
				repairProductId: null
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {
				dispatchName: [{ required: true, message: "发文名称不能为空", trigger: "blur" }],
				fileName: [{ required: true, message: "文件名称不能为空", trigger: "blur" }],
				dispatchTime: [{ required: true, message: "请选择发文日期", trigger: "blur" }],
				department: [{ required: true, message: "主送部门不能为空", trigger: "blur" }],
				salesType: [{ required: true, message: "请选择发文类型", trigger: "change" }]
			},
			options: [
				{ label: "服务保障", value: "1" },
				{ label: "维修", value: "2" }, // !!!!!!!!!维修不能新增
				{ label: "销售", value: "3" }
			],
			tableData: [],
			//查询结算列表
			settlementList: [],
			settlementListTotal: 0,
			queryParamsSettlementList: {
				pageNum: 1,
				pageSize: 10
			},
			dialogVisible: false,
			settlementSelectedIDS: []
		}
	},
	created() {
		this.getSize()
	},
	activated(){
		this.getSize()
	},
	methods: {
		getSize() {
			getcolumn({ pageName: "Pagination58" }).then(response => {
				this.queryParams.pageSize = response.data.length ? (response.data[0].limit ? response.data[0].limit : 10) : 10
				this.getList()
			})
		},
		changeTableData() {
			this.$nextTick(() => {
				this.$refs.table.doLayout()
			})
		},
		// 单击表格行
		row_click(row, column, event) {
			if (column && column.label == "操作") {
				return
			} else {
				this.$refs.table.toggleRowSelection(row)
			}
		},
		/** 查询销售发文单列表 */
		getList() {
			this.loading = true
			listSalesDocument(this.queryParams).then(response => {
				this.salesDocumentList = response.rows
				this.total = response.total
				this.loading = false
			})
		},
		//每次切换设备类型时清空下方填写的数据
		changeSalesType() {
			this.tableData = []
		},
		//删除行
		deleteRow(idx) {
			this.tableData.splice(idx, 1)
		},
		//增加行
		addOne() {
			if (this.form.salesType === "1") {
				this.tableData.push({
					serviceName: "",
					serviceUser: "",
					serviceDay: "",
					serviceContent: "",
					serviceAfterSale: "", //非必填
					tripMode: "",
					serviceCost: "", //非必填
					travellingExpenses: "", //非必填
					totalCost: "",
					buyerName: "",
					buyerParagraph: "",
					remark: ""
				})
			} else {
				this.tableData.push({
					productName: "",
					contractNumber: "",
					productModel: "",
					serialNumber: "",
					measuringUnit: "",
					unitPrice: "",
					quantity: "",
					contractAmount: "",
					tax: "",
					buyerName: "",
					buyerParagraph: "",
					remark: ""
				})
			}
		},
		//点击查询结算按钮
		openSecondDialog() {
			this.queryParamsSettlementList.pageNum = 1
			this.settlementListTotal = 0
			this.getData()
			this.dialogVisible = true
			this.settlementSelectedIDS = []
			this.$nextTick(() => {
				this.$refs.multiselectTable.clearSelection()
			})
		},
		async getData() {
			const res = await getSettlementList()
			// console.log("res", res)
			this.settlementList = res.rows
			this.settlementListTotal = res.total
			this.$nextTick(() => {
				this.settlementList.forEach(item => {
					// 重点： 在当前分页列表中查询与回显数据是否有一致的id，一致则勾选数据回显
					// toggleRowSelection(item, true)：设置当前行数据为选中状态
					if (this.settlementSelectedIDS.includes(item.id)) {
						this.$refs.multiselectTable.toggleRowSelection(item, true)
					}
				})
			})
		},
		// 取消按钮
		cancel() {
			this.open = false
			this.reset()
			this.tableData = []
		},
		// 表单重置
		reset() {
			this.form = {
				id: null,
				dispatchName: null,
				fileName: null,
				dispatchTime: null,
				department: null,
				salesType: null
			}
			this.resetForm("form")
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1
			this.getList()
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm")
			this.handleQuery()
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.id)
			this.single = selection.length !== 1
			this.multiple = !selection.length
		},
		handleClose(down) {
			this.$refs.multiselectTable.clearSelection()
			down()
		},
		// 设备多选事件
		handleDeviceSelectionChange(selection, row) {
			if (!this.settlementSelectedIDS.includes(row.id)) {
				this.settlementSelectedIDS.push(row.id)
			} else {
				this.settlementSelectedIDS.forEach((id, index) => {
					if (id === row.id) {
						this.settlementSelectedIDS.splice(index, 1)
					}
				})
			}
		},
		// 设备全选事件
		handleDeviceAllChange(selection) {
			if (selecteds.length > 0) {
				selecteds.forEach(item => {
					if (!this.settlementSelectedIDS.includes(item.id)) {
						this.settlementSelectedIDS.push(item.id)
					}
				})
			} else {
				this.settlementList.forEach(item => {
					this.settlementSelectedIDS.forEach((id, index) => {
						if (id === item.id) {
							this.settlementSelectedIDS.splice(index, 1)
						}
					})
				})
			}
		},
		//查询结算弹窗的确定按钮
		async submitSeletedItem() {
			if (!this.settlementSelectedIDS.length) {
				this.$modal.msgError("请选择结算单")
				return
			}
			const res = await getSublist(this.settlementSelectedIDS)
			// console.log("res", res)
			this.tableData = res.data
			this.dialogVisible = false
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset()
			this.open = true
			this.title = "添加销售发文单"
			this.tableData = []
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset()
			const id = row.id || this.ids
			getSalesDocument(id).then(response => {
				this.form = response.data
				this.open = true
				this.title = "修改销售发文单"
				if (this.form.salesType === "1") {
					this.tableData = response.data.tsalesDocumentServeDetails
				} else {
					this.tableData = response.data.tsalesDocumentMarketDetails
				}
			})
		},
		/** 查看按钮操作 */
		handleView(row) {
			this.reset()
			const id = row.id || this.ids
			getSalesDocument(id).then(response => {
				this.form = response.data
				this.open = true
				this.title = "查看销售发文单"
				if (this.form.salesType === "1") {
					this.tableData = response.data.tsalesDocumentServeDetails
				} else {
					this.tableData = response.data.tsalesDocumentMarketDetails
				}
			})
		},

		/** 提交按钮 */
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					const loading = this.$loading({
						lock: true,
						text: "Loading",
						spinner: "el-icon-loading",
						background: "rgba(0, 0, 0, 0.7)"
					})
					//粗略校验必填
					if (this.tableData.length > 0) {
						const keys = ["serviceName", "serviceUser", "serviceDay", "serviceContent", "tripMode", "totalCost", "buyerName", "buyerParagraph"]
						const keysTwo = ["productName", "contractNumber", "productModel", "serialNumber", "measuringUnit", "buyerName", "buyerParagraph"]
						for (let i = 0; i < this.tableData.length; i++) {
							const values = Object.values(this.tableData[i])
							console.log(values)
							const hasInvalidValue = values.some(value => value === "" || value === undefined || value !== value || value == null || value.toString().trim().length === 0)
							if (hasInvalidValue) {
								for (let key in this.tableData[i]) {
									if (this.tableData[i][key] === "" || this.tableData[i][key] === undefined || this.tableData[i][key] == null || this.tableData[i][key] !== this.tableData[i][key] || this.tableData[i][key].toString().trim().length === 0) {
										if (this.form.salesType === "1") {
											if (keys.includes(key)) {
												this.$notify.error({
													title: "错误",
													message: `请补充完整第${i + 1}行数据`
												})
												return
											}
										} else {
											if (keysTwo.includes(key)) {
												this.$notify.error({
													title: "错误",
													message: `请补充完整第${i + 1}行数据`
												})
												return
											}
										}
									}
								}
							}
						}

						const params = {
							...this.form,
							tsalesDocumentServeDetails: this.form.salesType === "1" ? this.tableData : [],
							tsalesDocumentMarketDetails: ["2", "3"].includes(this.form.salesType) ? this.tableData : []
						}

						if (this.form.id != null) {
							updateSalesDocument(params)
								.then(response => {
									this.$modal.msgSuccess("修改成功")
									this.open = false
									this.getList()
									loading.close()
								})
								.finally(() => {
									loading.close()
								})
						} else {
							addSalesDocument(params)
								.then(response => {
									this.$modal.msgSuccess("新增成功")
									this.open = false
									this.getList()
									loading.close()
								})
								.finally(() => {
									loading.close()
								})
						}
					} else {
						if (this.form.id != null) {
							updateSalesDocument(this.form)
								.then(response => {
									this.$modal.msgSuccess("修改成功")
									this.open = false
									this.getList()
								})
								.finally(() => {
									loading.close()
								})
						} else {
							addSalesDocument(this.form)
								.then(response => {
									this.$modal.msgSuccess("新增成功")
									this.open = false
									this.getList()
								})
								.finally(() => {
									loading.close()
								})
						}
					}
				}
			})
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const ids = row.id || this.ids
			this.$modal
				.confirm("是否确认删除此项销售发文单？")
				.then(function () {
					return delSalesDocument(ids)
				})
				.then(() => {
					this.getList()
					this.$modal.msgSuccess("删除成功")
				})
				.catch(() => {})
		}
	}
}
</script>
<style lang="scss" scoped>
.right-content-box {
	width: 100%;
	min-height: 50vh;
	position: relative;
	.tip {
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		white-space: nowrap;
		overflow: hidden;
		font-size: 20px;
		color: #dcdfe6;
		letter-spacing: 2px;
	}
}

.action-bar {
	.tips {
		margin-left: 20px;
		font-size: 14px;
		color: #666;
		white-space: nowrap;
		overflow: hidden;
	}
}
</style>
